﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TimKen.App;
using TimKen.DataEntity;
using TimKen.BussLogic;

namespace TimKen.Web.User
{
    public partial class UserLogin : WebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(Request["UserName"]) && !string.IsNullOrEmpty(Request["Password"]))     
            {
                string username = string.IsNullOrEmpty(Request["UserName"]) ? "" : Request["UserName"];
                string password = string.IsNullOrEmpty(Request["Password"]) ? "" : Request["Password"];
                string codestring = string.IsNullOrEmpty(Request["TxtCode"]) ? "" : Request["TxtCode"];
                Session["intcode"] = Session["intcode"] == null ? 0 : Convert.ToInt32(Session["intcode"]) + 1;
                if (Convert.ToInt32(Request["intcode"]) >= 3)
                {
                    if (codestring != "" && codestring != GetLangString("YourCode"))
                    {
                        if (!Session["CheckCode"].ToString().ToUpper().Trim().Equals(codestring.ToUpper()))
                        {
                            Response.Write("7");
                            Response.End();
                            //Alert(GetLangString("x_CodeNotAllowBlank"), "/Index.aspx");
                            //验证码验证失败divMessageBox     
                        }
                    }
                    else
                    {
                        Response.Write("7");
                        Response.End();
                        //Alert(GetLangString("x_CodeNotAllowBlank"), "/Index.aspx");
                    }
                }
                UserS u = UserFacade.Login(username, password);

                if (u != null && u.UserType != UserType.DataAdministrator && u.UserType != UserType.SystemAdministrator)
                {
                    Session.Add("tempUserName", username);
                    Session["User"] = u;
                    if (!u.IsValid)
                    {                  //盘点是否邮箱验证完成  
                        Response.Write("6");
                        //Response.Redirect("/User/AfterRegister.aspx?action=1", false);
                    }
                    else
                    {
                        if (Request.QueryString["reurl"] != null)
                        {
                            if (!UserFacade.CheckUserPasswordExpired(u))
                            {
                                Response.Write("5");
                                Response.End();
                                //Response.Redirect("/User/UserAccountEdit.aspx?type=1");
                            }
                            else
                            {
                                Response.Write("4");
                                Response.End();
                            }
                                //Response.Redirect(Request.QueryString["reurl"]); //"/Product/Category.aspx");
                        }
                        else
                        {
                            if (u.UserType == UserType.SelfRegistered)
                            {
                                Response.Write("2");
                                Response.End();                        
                                //Response.Redirect("/Index.aspx", true);
                            }


                            if (!UserFacade.CheckUserPasswordExpired(u))
                            {
                                Response.Write("3");
                                Response.End();
                                //Response.Redirect("/User/UserAccountEdit.aspx?type=1");
                            }    
                            else
                            {
                                if (Request.Path.Trim().ToLower() == "/Index.aspx".ToLower() || Request.Path.Trim().ToLower() == "/User/UserLogin.aspx".ToLower())    // "/User/login.aspx"
                                {
                                    Response.Write("2");
                                    Response.End();
                                    //Response.Redirect("/Index.aspx");
                                    //if (u.UserType == UserType.FactoryFinance)
                                    //    Response.Redirect("/Order/CheckAccount.aspx");
                                    //else
                                    //    Response.Redirect("/Order/OrderList.aspx");
                                }
                                else                                  
                                {
                                    Response.Write("1");
                                    Response.End();
                                    //Response.Redirect(Request.Path);
                                }                  
                            }
                        }
                    }
                }
                else
                {
                    Response.Write("0");
                    Response.End();
                    //Alert(GetLangString("ErrorLogin"), "/Index.aspx");
                }
            }
        }
    }
}